<?php

namespace app\common\model;

use think\Model;


class SafetyPlan extends Model
{

    

    

    // 表名
    protected $name = 'safety_plan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = "Y-m-d H:i:s";

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = "updatetime";
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'plan_target_user_info'
    ];

    public function getPlanTargetUserInfoAttr($value, $data) {
        if (empty($data['plan_target_ids'])) return [];
        return User::get($data["plan_target_ids"]);
    }
    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function log() {
        return $this->hasOne("SafetyPlanLog", "id", "log_id");
    }
}
